package com.shop.trade.atom.atomsvc.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shop.common.entity.trade.OrderBase;
import com.shop.common.model.InputBean;
import com.shop.common.model.ServiceBean;
import com.shop.common.model.param.trade.OrderBaseInput;
import com.shop.common.model.result.trade.OrderBaseOutput;
import com.shop.common.tmpl.BaseService;
import com.shop.trade.atom.atomsvc.IOrderAtomSvc;
import com.shop.trade.atom.dao.OrderBaseMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

@Service
public class OrderAtom extends BaseService implements IOrderAtomSvc {
    @Autowired
    OrderBaseMapper orderBaseMapper;

    @Override
    public ServiceBean<Integer> insertOrderBase(OrderBase orderBaseInput) {
        orderBaseInput.setBalance(orderBaseInput.getBalance()==null?new BigDecimal(0):orderBaseInput.getBalance());
        orderBaseInput.setPayPrice(orderBaseInput.getPayPrice()==null?new BigDecimal(0):orderBaseInput.getPayPrice());
        int i = orderBaseMapper.insertSelective(orderBaseInput);
        if (opSuccess(i)) {
            return resultData(orderBaseInput.getId());
        }
        return resultFailed();
    }

    @Override
    public ServiceBean<String> deleteOrderBase(Integer orderId) {
        int i = orderBaseMapper.deleteByPrimaryKey(orderId);
        if (opSuccess(i)) {
            return resultSuccess();
        }
        return resultFailed();
    }

    @Override
    public ServiceBean<OrderBaseOutput> selectOrderBase(OrderBaseInput orderBaseInput) {
        List<OrderBaseOutput> list = orderBaseMapper.qryOrderBase(orderBaseInput);
        if (list != null && list.size() > 0) {
            return resultData(list.get(0));
        }
        return resultFailed();
    }

    @Override
    public ServiceBean<OrderBaseOutput> selectOrderBaseById(Integer orderId) {
        OrderBaseOutput orderBaseOutput = orderBaseMapper.selectByPrimaryKey(orderId);
        if (orderBaseOutput != null) {
            return resultData(orderBaseOutput);
        }
        return resultFailed();
    }

    @Override
    public ServiceBean<List<OrderBaseOutput>> selectOrderBaseList(OrderBaseInput orderBaseInput) {
        List<OrderBaseOutput> list = orderBaseMapper.qryOrderBase(orderBaseInput);
        if (list != null && list.size() > 0) {
            return resultData(list);
        } else {
            return resultFailed();
        }
    }

    @Override
    public ServiceBean<List<OrderBaseOutput>> selectOrderBaseList4P(InputBean<OrderBaseInput> orderBaseInput) {
        PageHelper.startPage(orderBaseInput.getIndex(), orderBaseInput.getPageSize());
        List<OrderBaseOutput> list = orderBaseMapper.qryOrderBase(orderBaseInput.getParam());
        PageInfo<OrderBaseOutput> pageInfo = new PageInfo<>(list);
        ServiceBean<List<OrderBaseOutput>> serviceBean = new ServiceBean<>();
        serviceBean.setSuccess(true);
        serviceBean.setData(pageInfo.getList());
        serviceBean.setMaxSize(pageInfo.getTotal());
        return serviceBean;
    }

    @Override
    public ServiceBean<Integer> updateOrderBase(OrderBase orderBase) {
        int i = orderBaseMapper.updateByPrimaryKeySelective(orderBase);
        if (opSuccess(i)) {
            return resultSuccess();
        }
        return resultFailed();
    }

}
